

#include "GLRender.h"


//
GLRender::GLRender( )
{
	m_hWnd			= NULL;
	m_hDC			= NULL;
	m_hGLRC			= NULL;

	//
	m_bInitialize	= FALSE;

}

GLRender::~GLRender(void)
{
	ExitRender( );
}

//
BOOL	GLRender::ExitRender( )
{
	if( !m_bInitialize ){ return TRUE; }
	m_bInitialize = FALSE;

	//
	wglMakeCurrent( NULL, NULL );
	wglDeleteContext( m_hGLRC );
	ReleaseDC( m_hWnd, m_hDC );

	//
	m_hGLRC		= NULL;
	m_hDC		= NULL;
	m_hWnd		= NULL;
	printf_s( "GLRender uninitialize success. \r\n");
	return TRUE;
}

//
BOOL	GLRender::Supported( )
{
	if (GLEW_VERSION_1_1/*glewIsSupported("GL_VERSION_1_1")*/)  
	{    
		printf_s("OpenGL 1.1 supported.\r\n");  
	}
	if (GLEW_VERSION_1_2/*glewIsSupported("GL_VERSION_1_2")*/)  
	{    
		printf_s("OpenGL 1.2 supported.\r\n");  
	}
	if (GLEW_VERSION_1_3/*glewIsSupported("GL_VERSION_1_3")*/)  
	{    
		printf_s("OpenGL 1.3 supported.\r\n");  
	}
	if (GLEW_VERSION_1_4/*glewIsSupported("GL_VERSION_1_4")*/)  
	{    
		printf_s("OpenGL 1.4 supported.\r\n");  
	}
	if (GLEW_VERSION_1_5/*glewIsSupported("GL_VERSION_1_5")*/)  
	{    
		printf_s("OpenGL 1.5 supported.\r\n");  
	}
	if (GLEW_VERSION_2_0/*glewIsSupported("GL_VERSION_2_0")*/)  
	{    
		printf_s("OpenGL 2.0 supported.\r\n");  
	}
	if (GLEW_VERSION_2_1/*glewIsSupported("GL_VERSION_2_1")*/)  
	{    
		printf_s("OpenGL 2.1 supported.\r\n");  
	}
	if (GLEW_VERSION_3_0/*glewIsSupported("GL_VERSION_3_0")*/)  
	{    
		printf_s("OpenGL 3.0 supported.\r\n");  
	}
	if (GLEW_VERSION_3_1/*glewIsSupported("GL_VERSION_3_1")*/)  
	{    
		printf_s("OpenGL 3.1 supported.\r\n");  
	}
	if (GLEW_VERSION_3_2/*glewIsSupported("GL_VERSION_3_2")*/)  
	{    
		printf_s("OpenGL 3.2 supported.\r\n");  
	}
	if (GLEW_VERSION_3_3/*glewIsSupported("GL_VERSION_3_3")*/)  
	{    
		printf_s("OpenGL 3.3 supported.\r\n");  
	}
	if (GLEW_VERSION_4_0/*glewIsSupported("GL_VERSION_4_0")*/)  
	{    
		printf_s("OpenGL 4.0 supported.\r\n");  
	}
	if (GLEW_VERSION_4_1/*glewIsSupported("GL_VERSION_4_0")*/)  
	{    
		printf_s("OpenGL 4.1 supported.\r\n");  
	}
	if (GLEW_VERSION_4_2/*glewIsSupported("GL_VERSION_4_0")*/)  
	{    
		printf_s("OpenGL 4.2 supported.\r\n");  
	}

	//
	printf_s( "Vendor:%s\r\n", glGetString(GL_VENDOR) );
	printf_s( "Renderer:%s\r\n", glGetString(GL_RENDERER) );
	printf_s( "Version:%s\r\n", glGetString(GL_VERSION) );
	return TRUE;
}

//
BOOL	GLRender::InitRender( HWND hWnd, long ColorBits )
{
	//
	m_hWnd			= hWnd;
	m_hDC			= GetDC( m_hWnd );

	//
	PIXELFORMATDESCRIPTOR pfd;
	ZeroMemory( &pfd, sizeof( PIXELFORMATDESCRIPTOR ) );
	pfd.nSize			= sizeof( PIXELFORMATDESCRIPTOR );
	pfd.nVersion		= 1;
	pfd.dwFlags			= PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
	pfd.iPixelType		= PFD_TYPE_RGBA;
	pfd.cColorBits		= (BYTE)ColorBits;
	pfd.cDepthBits		= 16;
	pfd.cStencilBits	= 1;
	pfd.iLayerType		= PFD_MAIN_PLANE;
	
	//
	int format = ChoosePixelFormat( m_hDC, &pfd );
	SetPixelFormat( m_hDC, format, &pfd );

	//
	m_hGLRC				= wglCreateContext( m_hDC );
	if( !m_hGLRC ){ return FALSE; }
	wglMakeCurrent( m_hDC, m_hGLRC );


	//
	GLenum		result = glewInit( );
	if( result != GLEW_OK)
	{ 
		printf_s( "glewInit error[%08X] %s \r\n", result, glewGetErrorString( result ) );
		return FALSE; 
	}
	if( !Supported( ) )
	{ 
		printf_s( "OpenGL support fail. \r\n");
		return FALSE; 
	}


	//
	m_bInitialize		= TRUE;
	printf_s( "GLRender initialize success. \r\n");
	return TRUE;
}

//
int		GLRender::OnRender( )
{
	if( !m_bInitialize ){ return 1; }
	if( !OnBeginRender( ) ){ return 0; }

	return OnEndRender( );
}

//
BOOL	GLRender::OnBeginRender( )
{
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

	//
	return TRUE;
}

//
BOOL	GLRender::OnEndRender( )
{
	glFinish( );
	SwapBuffers( m_hDC );
	return TRUE;
}